package org.jeecg.modules.tt_question_collect.util;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.tt_question_collect.controller.JiraApiController;
import org.jeecg.modules.tt_question_collect.service.ITtQuestionCollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

//定时任务类
@Slf4j
@Component
@RestController
@RequestMapping("/scheduletask/jirazt")
public class ScheduleTask {

    @Autowired
    private JiraApiController jiraApiController;

    @Autowired
    private ITtQuestionCollectService ttQuestionCollectService;


    // 每隔5秒执行一次
//    @Scheduled(cron = "0 41 15 ? * FRI")
//    public void printSay() {
//        System.out.println("周五下午四点执行一次：" + new Date());
//    }

    //定时同步任务到jira
//	@Scheduled(cron = "0 0 15 ? * 6")
//	 public void jiraquartztb() {
//		 List<Map<String,Object>> list = ttQuestionCollectService.getjiratblist();
//		 if (list.isEmpty()){
//               log.info("暂无可同步的jiar任务");
//		 }else {
//			 for (int a = 0;a< list.size();a ++){
//				 String id = String.valueOf(list.get(a).get("ID"));
//				 System.out.println(id);
//				 jiraApiController.createIssue(id);
//			 }
//		 }
//	 }



    //定时更新同步jira
	@Scheduled(cron = "0 0 6 ? * MON")
    @AutoLog(value = "一键jira同步更新")
    @ApiOperation(value="一键jira同步更新", notes="一键jira同步更新")
    @PostMapping(value = "/yjjiratbgx")
	 public   void jiraquarupdatezt() {

        List<Map<String,Object>> list = ttQuestionCollectService.queryupdatejira();
//        List<Map<String,Object>> tblists = ttQuestionCollectService.getjiratblist();
//
//        if (tblists.isEmpty()){
//            log.info("暂无可同步的jiar任务");
//        }else {
//            for (int i = 0;i< tblists.size();i ++){
//                String id = String.valueOf(tblists.get(i).get("ID"));
//                System.out.println(id);
//                jiraApiController.createIssue(id);
//                log.info(String.format(" 同步jira任务 普通定时任务 问题编号:"+list.get(i).get("QUESTIONCODE")+" 成功!  时间:" + DateUtils.getTimestamp()));
//
//            }
//        }

		 for (int a = 0;a< list.size();a ++){
			 String id = String.valueOf(list.get(a).get("ID"));
			 System.out.println(id);
             jiraApiController.getIssue(id);
             log.info(String.format(" 更新jira状态 普通定时任务 问题编号:"+list.get(a).get("QUESTIONCODE")+" 成功!  时间:" + DateUtils.getTimestamp()));
		 }

//        log.info("jira同步完成！共同步"+tblists.size()+"记录  时间:" + DateUtils.getTimestamp());
        log.info("更新jira同步状态完成！共更新"+list.size()+"记录 时间:" + DateUtils.getTimestamp());
	 }


}
